<?php
include_once('CLASS/autoload.php');
$site = new page_base('tableau');
$site->style = "styletable";
include_once('connexpdo.php');
//include_once('CTRL/CTRL-tableaux.php');
$varx=connexpdo("ppetesttab");
$requete = 'SELECT * FROM eleve';
$tab = $varx->query($requete);
$a="";
$b="";
$r="";
$c = "";
$AutorisationChecked = "";
$DroitImageChecked = "";
$CertificatChecked = "";
$v = "

<div class=\"table-title\">
<center><h3>Gestion Eleve</h3></center>
</div>
<table class=\"table-fill\">
<thead>
<tr>
<th class=\'text-left\'>Nom<br><input id='SearchNom' type='text' placeholder='Nom'/></th>
<th class=\"text-left\">Prenom<br><input id='SearchNom' type='text' placeholder='Nom'/></th>
<th class=\"text-left\">Date de Naissance<br><input id='SearchNom' type='text' placeholder='Nom'/></th>
<th class=\'text-left\'>Classement (A ou B)<br><FORM>
    				<SELECT id='SearchClassement' name='nom' size='1'>
    					<OPTION '.$a.' value='A' >A</OPTION>
    					<OPTION '.$b.' value='B' >B</OPTION>
    					<OPTION '.$r.' value='R' >Redoublant</OPTION>
    				</SELECT>
    			</FORM></th>
<th class=\"text-left\">Autorisation Parent</th>
<th class=\"text-left\">Certificat</th>
<th class=\"text-left\">Droit Image</th>
<th class=\"text-left\">Num Licence</th>
<th class=\"text-left\"></th>
</tr>
</thead>
<tbody class=\"table-hover\">";

while ($row = $tab->fetch(PDO::FETCH_OBJ))
{
	if($row->Classement == "A")
	{$a="selected";}
	else if($row->Classement == "B")
	{$b="selected";}
	else if($row->Classement == "R")
	{$r="selected";}
	else
	{}
	if ($row->AutorisationParent==0 || $row->AutorisationParent== NULL){$AutorisationChecked = "";}
	else {$AutorisationChecked = "Checked";}

	if ($row->Certificat==0 || $row->Certificat== NULL){$CertificatChecked = "";}
	else {$CertificatChecked = "Checked";}
	
	if ($row->DroitImage==0 || $row->DroitImage== NULL){$DroitImageChecked = "";}
	else {$DroitImageChecked = "Checked";}
	
	$c = $c."
			<tr>
			<td class='text-left'><input id='txtnom".$row->ID."' type='text' name='Nom' value='$row->Nom' disabled/></td>
			<td class='text-left'><input id='txtprenom".$row->ID."' type='text' name='Prenom' value='$row->Prenom' disabled/></td>
			<td class='text-left'><input id='datenaiss".$row->ID."' type='date' name='DateNaiss' value='$row->DateNaiss' disabled/></td>
			<td class='text-center'>
				<FORM>
    				<SELECT id='lb".$row->ID."' name='nom' size='1' disabled='disabled'>
    					<OPTION ".$a." value='A' >A</OPTION>
    					<OPTION ".$b." value='B' >B</OPTION>
    					<OPTION ".$r." value='R' >Redoublant</OPTION>
    				</SELECT>
    			</FORM>
    		</td>
			<td class='text-center'>
				<input id='autorisation".$row->ID."' type='checkbox' name='autorisation' $AutorisationChecked disabled='disabled'><label for='autorisation".$row->ID."'><span class='ui'></span></label>
			</td>
			<td class='text-center'><p><input id='certificat".$row->ID."' type='checkbox' name='certificat' $CertificatChecked disabled><label for='certificat".$row->ID."'><span class='ui'></span></label></p></td>
			<td class='text-center'><p><input id='droitimage".$row->ID."' type='checkbox' name='droitimage' $DroitImageChecked disabled><label for='droitimage".$row->ID."'><span class='ui'></span></label></p></td>
				
				<td class='text-left'><input id='txtnumlicence".$row->ID."' type='text' name='numlicence' value='$row->NumLicence' disabled/></td>
				<td class='text-center modifier'><input type='button' value='Modifier' id='btn".$row->ID."'></input></td>
					<script>
					function getXMLHttpRequest() {
						var xhr = null;

						if (window.XMLHttpRequest || window.ActiveXObject) 
						{
						if (window.ActiveXObject) 
						{
							try {xhr = new ActiveXObject('Msxml2.XMLHTTP');}
							catch(e) {xhr = new ActiveXObject('Microsoft.XMLHTTP');}
						} 
						
						else 
						{
							xhr = new XMLHttpRequest();
						}
						} 
						
						else 
						{			
						return null;
						}
						return xhr;
						}
						
					document.getElementById('btn$row->ID').onclick = function()
					{
						if (document.getElementById('btn$row->ID').value == 'Modifier')
						{
							document.getElementById('certificat$row->ID').disabled = false;
							document.getElementById('autorisation$row->ID').disabled = false;
							document.getElementById('droitimage$row->ID').disabled = false;
							document.getElementById('lb$row->ID').disabled = false;
							document.getElementById('txtnom$row->ID').disabled = false;
							document.getElementById('txtprenom$row->ID').disabled = false;
							document.getElementById('datenaiss$row->ID').disabled = false;
							document.getElementById('txtnumlicence$row->ID').disabled = false;
							document.getElementById('btn$row->ID').value = 'Valider';
						}
						
						else if (document.getElementById('btn$row->ID').value == 'Valider')
						{
 							if (confirm('Enregistrer les modifications ?')) 
 							{
 								
 								var IDEl = $row->ID;
 								var nom	 = document.getElementById('txtnom$row->ID').value;
 								var prenom = document.getElementById('txtprenom$row->ID').value;
 								var dateNaiss = document.getElementById('datenaiss$row->ID').value;
 								var classement = document.getElementById('lb$row->ID').value;
 								var autorisation = document.getElementById('autorisation$row->ID').value;
 								var certificat = document.getElementById('certificat$row->ID').value;
 								var droitimage = document.getElementById('droitimage$row->ID').value;
 								var numlicence = document.getElementById('txtnumlicence$row->ID').value;
      							
      							var xhr = getXMLHttpRequest();
      							
								xhr.open(\"POST\", \"modifeleve.php\", true);
								xhr.setRequestHeader(\"Content-Type\", \"application/x-www-form-urlencoded\");
								xhr.send(\"IDEl=\"+ IDEl +\"&nom=\"+ nom +\"&prenom=\"+ prenom +\"&dateNaiss=\"+ dateNaiss +\"&classement=\"+ classement +\"&autorisation=\"+ autorisation +\"&certificat=\"+ certificat +\"&droitimage=\"+ droitimage +\"&numlicence=\"+ numlicence);
    									
      							document.getElementById('certificat$row->ID').disabled = true;
								document.getElementById('autorisation$row->ID').disabled = true;
								document.getElementById('droitimage$row->ID').disabled = true;
								document.getElementById('lb$row->ID').disabled = true;
								document.getElementById('txtnom$row->ID').disabled = true;
								document.getElementById('txtprenom$row->ID').disabled = true;
								document.getElementById('datenaiss$row->ID').disabled = true;
								document.getElementById('txtnumlicence$row->ID').disabled = true;
								document.getElementById('btn$row->ID').value = 'Modifier';
								
    								}
  								}
							};
					
					</script>
				</tr>";
				$r="";
				$b="";
				$a="";
}
$site->corps = $v.$c."</tbody>
</table>";
$site->titre = "Tableau Affichage";
$site->affiche();
?>